package com.glory.hzlionpda.adapter;

import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.hzlionpda.R;
import com.glory.hzlionpda.bean.InputDataBean;

import java.util.List;

/**
 * @author
 * @date 2017/9/11
 * @desc
 */

public class InputDataListAdapter extends BaseQuickAdapter<InputDataBean, BaseViewHolder> {

    public InputDataListAdapter(@Nullable List<InputDataBean> data) {
        super(R.layout.adapter_input_list, data);
    }


    @Override
    protected void convert(BaseViewHolder helper, final InputDataBean item) {
        helper.setText(R.id.tvTitle, item.getXMMS());
        EditText editText = helper.getView(R.id.etContent);
        LinearLayout llCheck = helper.getView(R.id.llCheck);
        LinearLayout llChoose = helper.getView(R.id.llChoose);
        switch (item.getXMSTYPE()) {
            case "NUMBER":
                editText.setText(item.getXMMVALUE());
                editText.setVisibility(View.VISIBLE);
                llCheck.setVisibility(View.GONE);
                llChoose.setVisibility(View.GONE);
                editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                setEditListener(editText, item);
                break;
            case "STRING":
                editText.setText(item.getXMMVALUE());
                editText.setVisibility(View.VISIBLE);
                llCheck.setVisibility(View.GONE);
                llChoose.setVisibility(View.GONE);
                editText.setInputType(InputType.TYPE_CLASS_TEXT);
                setEditListener(editText, item);
                break;
            case "Yes or No":
                editText.setVisibility(View.GONE);
                llCheck.setVisibility(View.VISIBLE);
                llChoose.setVisibility(View.GONE);
                final RadioButton rbOK = helper.getView(R.id.rbOK);
                final RadioButton rbNG = helper.getView(R.id.rbNG);
                switch (item.getXMMVALUE()) {
                    case "YES":
                        rbOK.setChecked(true);
                        rbNG.setChecked(false);
                        break;
                    case "NO":
                        rbOK.setChecked(false);
                        rbNG.setChecked(true);
                        break;
                    default:
                        rbOK.setChecked(false);
                        rbNG.setChecked(false);
                        break;
                }
                rbOK.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        item.setXMMVALUE("YES");
                    }
                });
                rbNG.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        item.setXMMVALUE("NO");
                    }
                });
                break;
            case "3":
//                editText.setVisibility(View.GONE);
//                llCheck.setVisibility(View.GONE);
//                llChoose.setVisibility(View.VISIBLE);
//                if (item.getLANGUAGETYPE().equals("A")) {
//                    etChooseOther.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED | InputType.TYPE_NUMBER_FLAG_DECIMAL);
//                } else {
//                    etChooseOther.setInputType(InputType.TYPE_CLASS_TEXT);
//                }
//                setEditListener(etChooseOther, item);
//                tvChoose.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        final GeneralPop titlePop2 = new GeneralPop(mContext, popView
//                                , tvChoose.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
//                        String otherStr = item.getDATADETAIL() + ";其他";
//                        String[] strings = otherStr.split(";");
//                        final List<String> list = Arrays.asList(strings);
//                        popDownAreaListAdapter.setNewData(list);
//                        popDownAreaListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//                            @Override
//                            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                                popDownAreaListAdapter.setCurrentSelect(position);
//                                popDownAreaListAdapter.notifyDataSetChanged();
//                                tvChoose.setText(list.get(position));
//                                if (list.get(position).equals("其他")) {
//                                    etChooseOther.setVisibility(View.VISIBLE);
//                                } else {
//                                    etChooseOther.setVisibility(View.GONE);
//                                    item.setDATAVALUE(list.get(position));
//                                }
//                                titlePop2.dismiss();
//                            }
//                        });
//                        titlePop2.showAsDropDown(tvChoose, 0, 0);
//                    }
//                });
                break;
        }
    }

    private void setEditListener(EditText editText, final InputDataBean item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }
        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setXMMVALUE(s.toString());
            }
        };
        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
